package repository

import (
	"gorm.io/gorm"

	"podSrv/domain/model"
)

func Paginate(page *model.Page) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if page.Pages == 0 {
			page.Pages = 1
		}

		switch {
		case page.PagePerNums > 100:
			page.PagePerNums = 100
		case page.PagePerNums <= 0:
			page.PagePerNums = 10
		}

		offset := (page.Pages - 1) * page.PagePerNums
		return db.Offset(int(offset)).Limit(int(page.PagePerNums))
	}
}
